<?php
class IM_C_Pub_Contact extends IM_C_Base {
	protected $o_contactForm;
	protected $o_beDiscoveredForm;
	
	public function start(){
		parent::start();
		parent::showNavi();
		parent::showBottomLeftNavi();
		$this->registry->template->s_pageTitle = 'Contact Us';
		$this->registry->template->s_pageTitleSide = '<h1>contact</h1>';
		$this->registry->template->s_mainContent = $this->getMainContent('contactForm');
		$this->registry->template->show('Public-Generic.php');
	}
	public function beDiscoveredStart(){
		parent::start();
		parent::showNavi();
		parent::showBottomLeftNavi();
		$this->cleanTemp();
		$this->registry->template->s_pageTitle = 'Be Discovered';
		$this->registry->template->s_pageTitleSide = '<h1>be discovered</h1>';
		$this->registry->template->s_mainContent = $this->getMainContent('beDiscovered');
		$this->registry->template->show('Public-Generic.php');
	}
	
	public function handleBeDiscoveredFormSubmit(){
		$o_beDiscoveredForm = $this->getBeDiscoveredForm();
		$o_beDiscoveredForm->validateForm();
		$arr_inputVals = $o_beDiscoveredForm->getInputValues();
		if(count($o_beDiscoveredForm->getFormErrors()) == 0){
			$s_mailBody = $this->formatMailBody($arr_inputVals['be-discovered-form-name'], $arr_inputVals['be-discovered-form-about-me'], $arr_inputVals['be-discovered-form-contact-no'], $arr_inputVals['be-discovered-form-email']);
			$this->sendMail($s_mailBody, 'be discovered page', 'BeDiscoveredContactYesNo');
			$this->cleanTemp();
		}else{
			$o_beDiscoveredForm->setFieldValue('be-discovered-form-name', $arr_inputVals['be-discovered-form-name']);
			$o_beDiscoveredForm->setFieldValue('be-discovered-form-email', $arr_inputVals['be-discovered-form-email']);
			$o_beDiscoveredForm->setFieldValue('be-discovered-form-contact-no', $arr_inputVals['be-discovered-form-contact-no']);
			$o_beDiscoveredForm->setFieldValue('be-discovered-form-about-me', $arr_inputVals['be-discovered-form-about-me']);
			$this->o_beDiscoveredForm = $o_beDiscoveredForm;
			
			//$this->beDiscoveredStart();
			parent::start();
			parent::showNavi();
			parent::showBottomLeftNavi();
			$this->registry->template->s_pageTitle = 'Be Discovered';
			$this->registry->template->s_pageTitleSide = '<h1>be discovered</h1>';
			$this->registry->template->s_mainContent = $this->getMainContent('beDiscovered');
			$this->registry->template->show('Public-Generic.php');
		}
		
	}
	
	public function beDiscoveredStatusSent(){
		parent::start();
		parent::showNavi();
		parent::showBottomLeftNavi();
		//$this->cleanTemp();
		$this->registry->template->s_pageTitle = 'Be Discovered';
		$this->registry->template->s_pageTitleSide = '<h1>be discovered</h1>';
		$this->registry->template->s_mainContent = '<h1>IM Agency</h1><p>Inquiry sent.</p><p>We will get in touch with you as soon as possible</p>';
		$this->registry->template->show('Public-Generic.php');
	}
	
	public function handleContactFormSubmit(){
		$o_contactForm = $this->getContactForm();
		$o_contactForm->validateForm();
		$arr_inputVals = $o_contactForm->getInputValues();
		if(count($o_contactForm->getFormErrors()) == 0){
			//$arr_inputVals = $o_contactForm->getInputValues();
			$s_mailBody = $this->formatMailBody($arr_inputVals['contact-us-form-name'], $arr_inputVals['contact-us-form-inquiry'], $arr_inputVals['contact-us-form-contact-no'], $arr_inputVals['contact-us-form-email']);
			$this->sendMail($s_mailBody, 'contact us page', 'IM_ContactUsMessageYesNo');
			
		}else{
			$o_contactForm->setFieldValue('contact-us-form-name', $arr_inputVals['contact-us-form-name']);
			$o_contactForm->setFieldValue('contact-us-form-email', $arr_inputVals['contact-us-form-email']);
			$o_contactForm->setFieldValue('contact-us-form-contact-no', $arr_inputVals['contact-us-form-contact-no']);
			$o_contactForm->setFieldValue('contact-us-form-inquiry', $arr_inputVals['contact-us-form-inquiry']);
			$this->o_contactForm = $o_contactForm;
			
			$this->start();
			/*
			parent::start();
			parent::showNavi();
			parent::showBottomLeftNavi();
			$this->registry->template->s_pageTitle = 'Contact Us';
			$this->registry->template->s_pageTitleSide = '<h1>contact</h1>';
			$this->registry->template->s_mainContent = $this->getMainContent('contactForm');
			$this->registry->template->show('Public-Generic.php');
			*/
		}
	}
	
	private function formatMailBody($s_inquiryFrom, $s_mailBody, $s_contactNum, $s_emailAdd){
		$s_mailContent = '<table border="1" cellpadding="8">';
			$s_mailContent .= '<tr>';
				$s_mailContent .= '<th>inquiry from:</th><td>'.$s_inquiryFrom.'</td>';
			$s_mailContent .= '</tr>';
			
			$s_mailContent .= '<tr>';
				$s_mailContent .= '<th>email address:</th><td>'.$s_emailAdd.'</td>';
			$s_mailContent .= '</tr>';
			
			$s_mailContent .= '<tr>';
				$s_mailContent .= '<th>contact #:</th><td>'.$s_contactNum.'</td>';
			$s_mailContent .= '</tr>';
			
			$s_mailContent .= '<tr>';
				$s_mailContent .= '<th>message:</th><td>'.$s_mailBody.'</td>';
			$s_mailContent .= '</tr>';
		$s_mailContent .= '</table>';
		
		return $s_mailContent;
	}
	
	private function sendMail($s_mailBody, $s_fromPart, $s_appToRedirect){
		FileLoader::getInstance()->loadFile(C_FW_fwPackage . 'libraries/phpmailer/class.smtp.php');
		FileLoader::getInstance()->loadFile(C_FW_fwPackage . 'libraries/phpmailer/class.phpmailer.php');
		
		$o_mail = new PHPMailer();
		//$o_mail->IsSMTP();  
		//$o_mail->Host     = "mail.imagency.ph"; // SMTP server
		$o_mail->IsMail();
		switch($s_appToRedirect){
			case 'BeDiscoveredContactYesNo':
				$o_mail->Subject  = "inquiry for \"be discovered\" at imagency.ph: ".$s_fromPart;
				//start adding attachments
				$s_currTempFolder = SessionHandler::getInstance()->getSessVarKey();
				if(file_exists(C_FW_ABSPATH.'/im_temp/'.$s_currTempFolder)){
					$s_currPicDir = C_FW_ABSPATH.'/im_temp/'.$s_currTempFolder;
					$arr_pictures = scandir($s_currPicDir);
					if(!empty($arr_pictures)){
						foreach($arr_pictures as $f_pic){
							if(is_file($s_currPicDir.'/'.$f_pic)){
								if(file_exists($s_currPicDir.'/'.$f_pic)){
									$o_mail->AddAttachment($s_currPicDir.'/'.$f_pic, $f_pic);
								}
							}
						}
					}
				}
				break;
			case 'IM_ContactUsMessageYesNo':
				$o_mail->Subject  = "inquiry for \"contact us\"at imagency.ph: ".$s_fromPart;
				break;
		}
		$o_mail->Body     = $s_mailBody;
		$o_mail->FromName = 'imagency mailer - ';
		$o_mail->From     = "messenger@imagency.ph";
		$o_mail->AddAddress("imagencymanila@gmail.com");
		
		$o_mail->IsHTML(true);
		//$o_mail->Send();
		try{
			$o_mail->Send();
		}catch(Exception $e){
			switch($s_appToRedirect){
				case 'BeDiscoveredContactYesNo':
					fw_util_redirect('BeDiscoveredContactYesNo');
					break;
				case 'IM_ContactUsMessageYesNo':
					fw_util_redirect('IM_ContactUsMessageYesNo');
					break;
			}
		}
		switch($s_appToRedirect){
			case 'BeDiscoveredContactYesNo':
				fw_util_redirect('BeDiscoveredContactYesNo');
				break;
			case 'IM_ContactUsMessageYesNo':
				fw_util_redirect('IM_ContactUsMessageYesNo');
				break;
		}
		
		/*
		if(!$o_mail->Send()){
			echo 'error';
		}else{
			echo 'sent';
		}
		*/
	}
	
	public function messageSendStatus(){
		parent::start();
		parent::showNavi();
		parent::showBottomLeftNavi();
		$this->registry->template->s_pageTitle = 'contact us';
		$this->registry->template->s_pageTitleSide = '<h1>contact</h1>';
		$this->registry->template->s_mainContent = '<h1>IM Agency</h1><p>Inquiry sent.</p><p>We will get in touch with you as soon as possible</p>';
		$this->registry->template->show('Public-Generic.php');
	}
	
	private function getMainContent($s_formName){
		$s_content = '<h1>IM Agency</h1>';
		switch ($s_formName){
			case 'beDiscovered':
				$s_formHtml = $this->getBeDiscoveredForm()->getFormHtml();
				break;
			case 'contactForm':
				$s_formHtml = $this->getContactForm()->getFormHtml();
				break;
			default:
				$s_formHtml = $this->getContactForm()->getFormHtml();
				break;
		}
		$s_content .= $s_formHtml; 
		return $s_content;
	}
	
	private function getContactForm(){
		if(!isset($this->o_contactForm) && $this->o_contactForm == null){
			$o_contactForm = new ClaycodeForm('contact-us-form');
			$this->o_contactForm = $o_contactForm;
		}else{
			$o_contactForm = $this->o_contactForm;
		}
		
		return $o_contactForm;
	}
	
	private function getBeDiscoveredForm(){
		if(!isset($this->o_beDiscoveredForm) && $this->o_beDiscoveredForm == null){
			$o_beDiscoveredForm = new ClaycodeForm('be-discovered-form');
			$this->o_beDiscoveredForm = $o_beDiscoveredForm;
			$this->makeTemp();
		}else{
			$o_beDiscoveredForm = $this->o_beDiscoveredForm;
		}
		
		return $o_beDiscoveredForm;
	}
	
	private function makeTemp(){
		$s_currTempFolder = SessionHandler::getInstance()->getSessVarKey();
		if(!file_exists(C_FW_ABSPATH.'/im_temp/'.$s_currTempFolder)){
			mkdir(C_FW_ABSPATH.'/im_temp/'.$s_currTempFolder, 0775);
		}
	}
	
	
	private function cleanTemp(){
		$s_currTempFolder = SessionHandler::getInstance()->getSessVarKey();
		if(file_exists(C_FW_ABSPATH.'/im_temp/'.$s_currTempFolder)){
			$s_currPicDir = C_FW_ABSPATH.'/im_temp/'.$s_currTempFolder;
			$arr_pictures = scandir($s_currPicDir);
			if(!empty($arr_pictures)){
				foreach($arr_pictures as $f_pic){
					if(is_file($s_currPicDir.'/'.$f_pic)){
						if(file_exists($s_currPicDir.'/'.$f_pic)){
							unlink($s_currPicDir.'/'.$f_pic);
						}
					}
				}
			}
			rmdir($s_currPicDir);
		}
	}
	
	//service
	public function myPics(){
		try{
			
		
		$s_relPath = SessionHandler::getInstance()->getSessVarKey();
		require(C_FW_fwPackage. 'libraries/jqueryFileUpload/php/' . 'upload.class.php');
		$arr_options = array(
				'param_name'=> 'be-discovered-form-pictures',
				'script_url'=> fw_util_getFullUrl(). '/BeDiscovered/myPics',
				'upload_dir'=> C_FW_ABSPATH.'/im_temp/' . $s_relPath . '/',
				'upload_url'=> C_FW_TRUEURL.'im_temp/' . $s_relPath . '/',
				'delete_type' => 'POST',
				'image_versions' => false,
				'accept_file_types' => '/\.(gif|jpe?g|png)$/i',
				'max_file_size' => 2097152,//2 mb
				'max_number_of_files'=> 2,
		);
			
		$upload_handler = new UploadHandler($arr_options);
			
		switch ($_SERVER['REQUEST_METHOD']) {
			case 'GET':
				$upload_handler->get();
				break;
			case 'POST':
				if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
					$upload_handler->delete();
				} else {
					$upload_handler->post();
					$arr_picInfo = $upload_handler->getCurrPicInfo();
				}
				break;
			default:
				header('HTTP/1.1 405 Method Not Allowed');
		}
		}catch(Exception $e){
			die($e);
		}
	}
}